package com.eightylegs.customer.sample;

import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XML80app {

	public static Element addAttribute(Element elem, String name, String re, String content) throws Exception
	{
		return addAttributeUnicode(elem, name, re, content, false);
	}
	
	public static Element addAttributeUnicode(Element elem, String name, String re, String content, Boolean withUnicode) throws Exception
	{
		Pattern p = Pattern.compile(re);
		Matcher m = p.matcher(content);
		
		if (m.find() && m.group(1).length() > 0) {
			if (withUnicode) {
				elem.setAttribute(name, Unicode.stringToUnicode(m.group(1).replaceAll("<.*?>"," ").replaceAll("\\\\'", "'").replaceAll("\t", " ").replaceAll("  *", " ").trim()));
			}
			else {
				elem.setAttribute(name, m.group(1).replaceAll("<.*?>"," ").replaceAll("\\\\'", "'").replaceAll("  *", " ").replaceAll("\t", " ").trim());
			}
		}
		
		return elem;
	}
	
	public static Element addAttribute(Element elem, String from, String to, String name, String re, String content) throws Exception
	{
		return addAttributeUnicode(elem, from, to, name, re, content, false);
	}

	public static Element addAttributeUnicode(Element elem, String from, String to, String name, String re, String content, Boolean withUnicode) throws Exception
	{
		String s = content.substring(content.indexOf(from) + from.length());
		s = s.substring(0, s.indexOf(to));
		
		elem = addAttributeUnicode(elem, name, re, s, withUnicode);
		
		return elem;
	}

	public static Element addAttribute(Element elem, String name, String re, String content, String if_not_found) throws Exception
	{
		Pattern p = Pattern.compile(re);
		Matcher m = p.matcher(content);
		
		if (m.find()) {
			elem.setAttribute(name, Unicode.stringToUnicode(m.group(1).replaceAll("<.*?>"," ").replaceAll("\\\\'", "'").replaceAll("\t", " ").replaceAll("  *", " ").trim()));
		}
		else {
			elem.setAttribute(name, if_not_found);
		}
		
		return elem;
	}
	
	public static Element addChildren(Document doc, Element elem, String name, String re, String content) throws Exception
	{
		Pattern p = Pattern.compile(re);
		Matcher m = p.matcher(content);
		
		while (m.find()) {
			Element child = doc.createElement(name);
			elem.appendChild(child);
			
			for (int i = 1; i + 1 <= m.groupCount(); i = i + 2) {
				child.setAttribute(Unicode.stringToUnicode(m.group(i).trim()), Unicode.stringToUnicode(m.group(i+1).trim()));
			}
		}
		
		return elem;
	}
	
	public static Element addChildren(Document doc, Element elem, String name, String[] attr, String re, String content) throws Exception
	{
		Pattern p = Pattern.compile(re);
		Matcher m = p.matcher(content);
		
		while (m.find()) {
			Element child = doc.createElement(name);
			elem.appendChild(child);
			
			for (int i = 1; i <= m.groupCount(); i++) {
				child.setAttribute(attr[i-1].trim(), Unicode.stringToUnicode(m.group(i).replaceAll("<.*?>"," ").replaceAll("\\\\'", "'").replaceAll("  *", " ").replaceAll("\t", " ").trim()));
			}
		}
		
		return elem;
	}
	
	public static Element addChildren(Document doc, Element elem, String from, String to, String name, String[] attr, String re, String content) throws Exception
	{
		String s = content.substring(content.indexOf(from) + from.length());
		s = s.substring(0, s.indexOf(to));
		
		elem = addChildren(doc, elem, name, attr, re, s);
		
		return elem;
	}

	public static String generateXML(Document doc) throws Exception
	{
		StringWriter out = new StringWriter();
		Transformer xform = TransformerFactory.newInstance().newTransformer();
		xform.setOutputProperty(OutputKeys.INDENT, "yes");
		xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		xform.transform(new DOMSource(doc), new StreamResult(out));
		return out.toString();
	}

}
